define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-daterangepicker'], function ($, undefined, Backend, Table, Form, daterangepicker) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'comment/rule/index' + location.search,
                    add_url: 'comment/rule/add',
                    edit_url: 'comment/rule/edit',
                    del_url: 'comment/rule/del',
                    multi_url: 'comment/rule/multi',
                    table: 'comment_rule',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'school.name', title: __('学校')},
                        {field: 'name', title: __('Name')},
                        {field: 'behavior_category_type_text', title: __('Behavior_category_type')},
                        {field: 'time_type_text', title: __('Time_type')},
                        {field: 'start_time_text', title: __('Start_time')},
                        {field: 'end_time_text', title: __('End_time')},
                        {field: 'note', title: __('日期')},
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                $('.daterange').daterangepicker({
                    timePicker: false,
                    autoUpdateInput: false,
                    timePickerSeconds: true,
                    timePicker24Hour: true,
                    autoApply: true,
                    locale: {
                        format: 'YYYY-MM-DD HH:mm:ss',
                        customRangeLabel: __("Custom Range"),
                        applyLabel: __("Apply"),
                        cancelLabel: __("Clear"),
                    },
                }, function (start, end) {
                    $(this.element).val(start.format(this.locale.format) + " - " + end.format(this.locale.format));
                    $(this.element).trigger('blur');
                });


                $("input[name='row[time_type]']").on('change', function () {
                    let $week = $('#week'),$customer = $('#customer');
                    $week.hide();
                    $customer.hide();
                    if ($(this).val() === '1') {
                        $week.show();
                    } else if ($(this).val() === '2') {
                        $customer.show();
                    }
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});